from openai import OpenAI
import json
import string
import random
import re
import log.logger as log

client = OpenAI(api_key='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')


def response_string(wenan, system_prompt):
    response = client.chat.completions.create(
        model="gpt-4-1106-preview",
        messages=[
            {'role': 'system', 'content': system_prompt},
            {'role': 'user', 'content': wenan}
        ],
        temperature=0.7,
    )
    res = response.choices[0].message.content
    json_pattern = r'```json([\s\S]*?)```'
    matches = re.search(json_pattern, res)
    if matches:
        extracted_json = matches.group(1)
    else:
        log.log_put(None, None, "未找到JSON内容")
    data = json.loads(extracted_json)
    return data['property']


if __name__ == "__main__":
    system_prompt = "通过提供的字母，每一个字母都是一个字的首字母组成一个微信名再结合自动匹配，名称要中文，风格偏青春，名称要简短，不能超过20个字，返回一个json对象,内容放在title属性里面"
    # 从所有字母（大小写）中随机选择一个字符
    random_number = random.randint(2, 20)
    random_letter = ''.join(random.choice(string.ascii_letters) for _ in range(random_number))

    print("随机字母:", random_letter)

    # 发送用户消息并获得模型回复
    #response = response_string(f"{random_letter}", system_prompt)
    logging.info(333)


def test_1():
    system_prompt = "通过提供的字母，每一个字母都是一个字的首字母组成一个微信名再结合自动匹配，名称要中文，风格偏青春，名称要简短，不能超过20个字，返回一个json对象,内容放在title属性里面"
    # 从所有字母（大小写）中随机选择一个字符
    random_number = random.randint(2, 20)
    random_letter = ''.join(random.choice(string.ascii_letters) for _ in range(random_number))

    print("随机字母:", random_letter)

    # 发送用户消息并获得模型回复
    response = response_string(f"{random_letter}", system_prompt)
    print(response)


def test_2():
    print("1")
